﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace LightCAD.Core
{
    public enum LcDataType
    {
        String,
        Float,
        Double,
        Int,
        Long,
        Bool,
        DateTime,
        Point2d,
        Point3d
    }
    public class ParameterDefinition
    {
        public string Name { get; set; }
        public string DisplayName { get; set; }
        public string Description { get; set; }
        public bool IsArray { get; set; }
        public LcDataType DataType { get; set; }
        public int Index { get; set; }

        public object DefaultValue { get; set; }
    }

    public class ParameterSetDefinition : KeyedCollection<string, ParameterDefinition>
    {
        protected override string GetKeyForItem(ParameterDefinition item)
        {
            return item.Name;
        }
        protected override void InsertItem(int index, ParameterDefinition item)
        {
            base.InsertItem(index, item);
            item.Index = index;
        }
        protected override void SetItem(int index, ParameterDefinition item)
        {
            base.SetItem(index, item);
            item.Index = index;
        }

        public LcParameterSet New()
        {
            return new LcParameterSet(this);
        }
    }
    public class LcParameterSet
    {
        public ParameterSetDefinition Definition { get; set; }

        public object[] Values { get; set; }

        public object this[int index]
        {
            get { return Values[index]; }
            set { Values[index] = value; }
        }
        public object this[string name]
        {
            get
            {
                var def = Definition[name];
                var value = Values[def.Index];
                if (value == null)
                {
                    value = def.DefaultValue;
                }
                return value;
            }
            set
            {
                var def = Definition[name];
                this.Values[def.Index] = value;
            }
        }
        public LcParameterSet(ParameterSetDefinition definition)
        {
            this.Definition = definition;
            Values = new object[Definition.Count];
        }

        public T GetValue<T>(int index)
        {
            var value = Values[index];
            if (value == null)
            {
                value = Definition[index].DefaultValue;
            }
            return (T)value;
        }
        public T GetValue<T>(string name)
        {
            var def = Definition[name];
            var value = Values[def.Index];
            if (value == null)
            {
                value = def.DefaultValue;
            }
            return (T)value;
        }
        public void SetValue(int index, object value)
        {
            this.Values[index] = value;
        }
        public void SetValue(string name, object value)
        {
            var def = Definition[name];
            this.Values[def.Index] = value;
        }
    }

}
